package chapter5.applicationcontextaware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.GenericXmlApplicationContext;

public class ShutdownHookBean implements ApplicationContextAware {
	private ApplicationContext ctx;

	@Override
	public void setApplicationContext(ApplicationContext ctx)
			throws BeansException {
		System.out.println("Into Method Test");
		if (ctx instanceof GenericXmlApplicationContext) {
			System.out.println("Into Register Hook");
			((GenericXmlApplicationContext) ctx).registerShutdownHook();
		}
	}

	public static void main(String[] args) {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:chapter5/applicationcontextaware/shutdownHook.xml");
		ctx.refresh();

		ShutdownHookBean bean = (ShutdownHookBean) ctx.getBean("bean1");
	}

}
